/*
 * @Author: szx
 * @Date: 2022-05-14 14:11:20
 * @LastEditTime: 2022-05-14 14:28:43
 * @Description:
 * @FilePath: \leetcode\面试题\02\05\05.js
 */
function ListNode(val) {
    this.val = val;
    this.next = null;
}

/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */
 var addTwoNumbers = function (l1, l2) {
    let add = 0;
    let prev = new ListNode(-1);
    const head = prev;
    while (l1 || l2) {
        const x = l1 ? l1.val : 0;
        const y = l2 ? l2.val : 0;
        const res = x + y + add;
        prev.next = new ListNode(res % 10);
        add = Math.floor(res / 10);
        l1 = l1 ? l1.next : l1;
        l2 = l2 ? l2.next : l2;
        prev = prev.next;
    }
    if (add == 1) prev.next = new ListNode(add);
    return head.next;
};
